﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;
using System.Data.SqlTypes;

public partial class Management_ProductEditor : PageBase2
{
    private CategoryManager m_CategoryManager = new CategoryManager();
    private BrandManager m_BrandManager = new BrandManager();
    private ProductSeriesManager m_SeriesManager = new ProductSeriesManager();
    private ProductManager m_ProductManager = new ProductManager();
    private RoleManager m_RoleManager = new RoleManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.SignInUser1 == null)
        {
            Session.Remove("SignInUser1");
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
            return;
        }
        if (!IsPostBack)
        {
            Cosmetic.DataEntity.User theUser = this.SignInUser1;
            if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
        }

        if (!IsPostBack)
        {
            BindDDL();
            //this.LoadFitCategory();
            //修改产品
            if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
            {
                this.btnInsertProduct.Visible = true;
                this.btnEditProduct.Visible = false;
            }
            //新增产品
            else
            {
                this.btnInsertProduct.Visible = false;
                this.btnEditProduct.Visible = true;
                this.LoadProduct(new Guid(this.Request.QueryString["PID"]));
            }
        }
        (this.Master.FindControl("ScriptManagerMaster") as ScriptManager).RegisterPostBackControl(btnUpload);
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    //this.LoadFitCategory();
        //    //修改产品
        //    if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
        //    {
        //        this.btnInsertProduct.Visible = true;
        //        this.btnEditProduct.Visible = false;
        //    }
        //    //新增产品
        //    else
        //    {
        //        this.btnInsertProduct.Visible = false;
        //        this.btnEditProduct.Visible = true;
        //        this.LoadProduct(new Guid(this.Request.QueryString["PID"]));
        //    }
        //}
    }

    private void LoadFitCategory()
    {
        Category theSkinTopCategory = this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.UserSkin);
        this.rptSkinFit.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(theSkinTopCategory).OrderBy(theCategory => theCategory.Name);
        this.rptSkinFit.DataBind();
        Category theHairTopCategory = this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.UserHair);
        this.rptHairFit.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(theHairTopCategory).OrderBy(theCategory => theCategory.Name);
        this.rptHairFit.DataBind();
    }

    private void LoadDDL(Guid aLv3CategoryId)
    {
        BindLv1DDL();
        Category aCategory = m_CategoryManager.GetCategoryById(aLv3CategoryId);

        ddlLv1Category.SelectedValue = aCategory.SupCategory.SupCategoryId.ToString();
        BindLv2DDL();

        ddlLv2Category.SelectedValue = aCategory.SupCategoryId.ToString();

        BindLv3DDL();
        ddlLv3Category.SelectedValue = aCategory.CategoryId.ToString();
    }
    private void LoadProduct(Guid aProductId)
    {
        Product theProduct = this.m_ProductManager.GetProductById(aProductId);
        this.txtCode.Text = theProduct.ProductCode;
        this.txtProductName.Text = theProduct.Name;
        //this.txtPrice.Text							= theProduct.Price.ToString();
        this.imgProductPic.ImageUrl = theProduct.PictureUrl;
        this.imgProductPic.Visible = true;

        LoadDDL(theProduct.ProductCategoryId);
        //this.ddlProductCategory.SelectedCategory = theProduct.ProductCategory;

        this.txtPrice.Text = Convert.ToString(theProduct.Price);
        this.TextBox1.Text = Convert.ToString(theProduct.eprice);
        this.txtWeight.Text = theProduct.weight.ToString();

        this.fckBody.Value = theProduct.Description;
        this.Image2.ImageUrl = theProduct.RecommendPicture1;
        this.Image3.ImageUrl = theProduct.RecommendPicture2;
        this.Image4.ImageUrl = theProduct.RecommendPicture3;
        this.Image5.ImageUrl = theProduct.RecommendPicture4;
        this.TextBox2.Text = theProduct.keywords;
        this.TextBox3.Text = theProduct.contetnes;
        txtPublishDate.Text = theProduct.PublishDate.ToString("yyyy-MM-dd");
    }

    private void LoadProductFit(Product aProduct)
    {
        List<ProductFit> theSkinFit = this.m_ProductManager.GetProductSkinFitByProduct(aProduct);
        this.SetProductFit(theSkinFit, this.rptSkinFit);

        List<ProductFit> theHairFit = this.m_ProductManager.GetProductHairFitByProduct(aProduct);
        this.SetProductFit(theHairFit, this.rptHairFit);
    }

    private void SetProductFit(List<ProductFit> theProductFitList, Repeater aRepeater)
    {
        foreach (RepeaterItem theItem in aRepeater.Items)
        {
            ProductFit theProductFit;
            Guid theCategoryId = new Guid((theItem.FindControl("hfCategoryId") as HiddenField).Value);
            TextBox theScore = theItem.FindControl("txtScore") as TextBox;
            if (null != (theProductFit = theProductFitList.Find(theFit => theFit.FitCategoryId == theCategoryId)))
            {
                theScore.Text = theProductFit.Score.ToString();
            }
        }
    }



    protected void cbCreateSubcategory_CheckedChanged(object sender, EventArgs e)
    {
        this.pnlMainCategory.Visible = !this.pnlMainCategory.Visible;
        this.ddlNewCategory.SelectedCategory = this.ddlProductCategory.SelectedCategory;
    }

    protected void btnInsertCategory_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(this.txtNewCategoryName.Text.Trim()))
        {
            this.ltlNeedCategoryName.Visible = true;
            return;
        }
        else
        {
            this.ltlNeedCategoryName.Visible = false;
        }

        Category theSupCategory;
        if (this.cbCreateSubCategory.Checked)
        {
            theSupCategory = this.ddlNewCategory.SelectedCategory;
        }
        else
        {
            theSupCategory = this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L);
        }

        if (!this.m_CategoryManager.CheckContains(theSupCategory, this.txtNewCategoryName.Text.Trim()))
        {
            Category theNewCategory = new Category();
            theNewCategory.CategoryId = Guid.NewGuid();
            theNewCategory.Name = this.txtNewCategoryName.Text.Trim();
            theNewCategory.CreateTime = DateTime.Now;
            theNewCategory.SupCategoryId = theSupCategory.CategoryId;
            theNewCategory.PictureUrl = this.txtShowPicturePath.Text.Trim();
            this.m_CategoryManager.Insert(theNewCategory);
            this.ddlProductCategory.SelectedCategory = theNewCategory;
            //this.mpeCategory.Hide();
        }
        else
        {
            this.ltlNeedCategoryName.Text = "Already have this category!";
            this.ltlNeedCategoryName.Visible = true;
        }
    }





    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //this.mpeCategory.Hide();

    }

    public static string UUID()
    {
        byte[] buffer = Guid.NewGuid().ToByteArray();
        //byte[] buffer = Origin.ToByteArray();
        long long_guid = BitConverter.ToInt64(buffer, 0);

        string _Value = System.Math.Abs(long_guid).ToString();

        byte[] buf = new byte[_Value.Length];
        int p = 0;
        for (int i = 0; i < _Value.Length; )
        {
            byte ph = System.Convert.ToByte(_Value[i]);

            int fix = 1;
            if ((i + 1) < _Value.Length)
            {
                byte pl = System.Convert.ToByte(_Value[i + 1]);
                buf[p] = (byte)((ph << 4) + pl);
                fix = 2;
            }
            else
            {
                buf[p] = (byte)(ph);
            }

            if ((i + 3) < _Value.Length)
            {
                if (System.Convert.ToInt16(_Value.Substring(i, 3)) < 256)
                {
                    buf[p] = System.Convert.ToByte(_Value.Substring(i, 3));
                    fix = 3;
                }
            }
            p++;
            i = i + fix;
        }
        byte[] buf2 = new byte[p];
        for (int i = 0; i < p; i++)
        {
            buf2[i] = buf[i];
        }
        string cRtn = System.Convert.ToBase64String(buf2);
        if (cRtn == null)
        {
            cRtn = "";
        }
        cRtn = cRtn.ToLower();
        cRtn = cRtn.Replace("/", "");
        cRtn = cRtn.Replace("+", "");
        cRtn = cRtn.Replace("=", "");
        if (cRtn.Length == 12)
        {
            return cRtn;
        }
        else
        {
            return UUID();
        }
    }


    protected void btnInsertProduct_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.txtCode.Text.Trim()))
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('insert product code!');", true);
            return;
        }
        if (this.txtCode.Text.Trim().Length > 7)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert(' product code error!');", true);
            return;
        }

        if (this.m_ProductManager.CheckExists(this.txtCode.Text.Trim()))
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('the product code is exist!');", true);
            return;
        }
        if (String.IsNullOrEmpty(this.txtProductName.Text.Trim()))
        {
            this.ltlNeedProductName.Visible = true;
            return;
        }
        else
        {
            this.ltlNeedProductName.Visible = false;
        }
        Product theNewProduct = new Product();
        theNewProduct.ProductCode = this.txtCode.Text.Trim();
        theNewProduct.ProductId = Guid.NewGuid();
        theNewProduct.UID = UUID();
        theNewProduct.Name = this.txtProductName.Text.Trim();
        //theNewProduct.ProductCategoryId = this.ddlProductCategory.SelectedCategory.CategoryId;
        if (!string.IsNullOrEmpty(ddlLv3Category.SelectedValue))
        {
            theNewProduct.ProductCategoryId = new Guid(this.ddlLv3Category.SelectedValue);
        }
        else
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('必须是3级分类!');", true);
            return;
        }
        //theNewProduct.ProductCategoryId = new Guid(this.ddlLv3Category.SelectedValue);
        theNewProduct.CreateTime = DateTime.Now;
        theNewProduct.CreateUserId = new Guid("07C684D3-910F-44FD-9781-2DEE610B59E8");
        theNewProduct.Description = this.fckBody.Value;
        theNewProduct.Price = 0;//Convert.ToDecimal(this.txtPrice.Text.Trim());
        theNewProduct.LastViewTime = DateTime.Now;
        theNewProduct.PictureUrl = this.imgProductPic.ImageUrl;
        theNewProduct.Size = this.txtSize.Text;
        theNewProduct.Views = 0;
        theNewProduct.RecommendPicture1 = this.Image2.ImageUrl;
        theNewProduct.RecommendPicture2 = this.Image3.ImageUrl;
        theNewProduct.RecommendPicture3 = this.Image4.ImageUrl;
        theNewProduct.RecommendPicture4 = this.Image5.ImageUrl;
        theNewProduct.ProductSeriesId = Guid.NewGuid();
        theNewProduct.BrandId = new Guid("4a5642fc-5f0b-4101-b25f-0e16fde728cc");
        theNewProduct.weight = int.Parse(string.IsNullOrEmpty(this.txtWeight.Text.Trim()) ? "0" : this.txtWeight.Text.Trim());

        if (txtPrice.Text.Trim() == "")
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('市场价格不能为空!');", true);
            return;
        }
        if (TextBox1.Text.Trim() == "")
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('真实价格不能为空!');", true);
            return;
        }

        theNewProduct.Price = Convert.ToDecimal(this.txtPrice.Text);//市场价格
        theNewProduct.eprice = Convert.ToDecimal(this.TextBox1.Text);//真实价格
        theNewProduct.procontents = "1";
        theNewProduct.counts = Convert.ToInt32("1");
        theNewProduct.keywords = this.TextBox2.Text;
        theNewProduct.contetnes = this.TextBox3.Text;
        DateTime thePublishDate;
        if (DateTime.TryParse(this.txtPublishDate.Text, out thePublishDate))
        {
            theNewProduct.PublishDate = thePublishDate;
        }
        else
        {
            theNewProduct.PublishDate = DateTime.Today;
        }
        List<ProductFit> theProductFitList = new List<ProductFit>();
        theProductFitList = this.GetProductFit(theProductFitList, this.rptHairFit);
        theProductFitList = this.GetProductFit(theProductFitList, this.rptSkinFit);
        this.m_ProductManager.Insert(theNewProduct, theProductFitList);


        #region 页面上已经隐藏。没有用
        ///将同一种产品写入多个类别
        if (cbCategory1.Checked)
        {
            Product theNewProduct1 = new Product();
            theNewProduct1.ProductCode = this.txtCode.Text.Trim();
            theNewProduct1.ProductId = Guid.NewGuid();
            theNewProduct1.Name = this.txtProductName.Text.Trim();
            theNewProduct1.ProductCategoryId = this.ddlProductCategory2.SelectedCategory.CategoryId;
            theNewProduct1.CreateTime = DateTime.Now;
            theNewProduct1.CreateUserId = new Guid("07C684D3-910F-44FD-9781-2DEE610B59E8");
            theNewProduct1.Description = this.fckBody.Value;
            theNewProduct1.Price = 0; //Convert.ToDecimal(this.txtPrice.Text.Trim());
            theNewProduct1.LastViewTime = DateTime.Now;
            theNewProduct1.PictureUrl = this.imgProductPic.ImageUrl;
            theNewProduct1.Size = this.txtSize.Text;
            theNewProduct1.Views = 0;
            theNewProduct1.RecommendPicture1 = this.Image2.ImageUrl;
            theNewProduct1.RecommendPicture2 = this.Image3.ImageUrl;
            theNewProduct1.weight = Convert.ToInt32(txtWeight.Text);
            theNewProduct1.Price = Convert.ToDecimal(this.txtPrice.Text);
            theNewProduct1.eprice = Convert.ToDecimal(TextBox1.Text);
            theNewProduct1.ProductSeriesId = Guid.NewGuid();
            theNewProduct1.BrandId = new Guid("4a5642fc-5f0b-4101-b25f-0e16fde728cc");
            theNewProduct.keywords = this.TextBox2.Text;
            theNewProduct.contetnes = this.TextBox3.Text;
            DateTime thePublishDate1;
            if (DateTime.TryParse(this.txtPublishDate.Text, out thePublishDate1))
            {
                theNewProduct1.PublishDate = thePublishDate1;
            }
            else
            {
                theNewProduct1.PublishDate = DateTime.Today;
            }
            //theNewProduct1.counts = 2;
            this.m_ProductManager.Insert(theNewProduct1);
        }
        if (cbCategory2.Checked)
        {
            Product theNewProduct2 = new Product();
            theNewProduct2.ProductCode = this.txtCode.Text.Trim();
            theNewProduct2.ProductId = Guid.NewGuid();
            theNewProduct2.Name = this.txtProductName.Text.Trim();

            theNewProduct2.ProductCategoryId = this.ddlProductCategory3.SelectedCategory.CategoryId;
            theNewProduct2.CreateTime = DateTime.Now;
            theNewProduct2.CreateUserId = new Guid("07C684D3-910F-44FD-9781-2DEE610B59E8");
            theNewProduct2.Description = this.fckBody.Value;
            theNewProduct2.Price = 0;
            theNewProduct2.LastViewTime = DateTime.Now;
            theNewProduct2.PictureUrl = this.imgProductPic.ImageUrl;
            theNewProduct2.Size = this.txtSize.Text;
            theNewProduct2.Views = 0;
            theNewProduct2.RecommendPicture1 = this.Image2.ImageUrl;
            theNewProduct2.RecommendPicture2 = this.Image3.ImageUrl;
            theNewProduct2.weight = Convert.ToInt32(txtWeight.Text);
            theNewProduct2.Price = Convert.ToDecimal(this.txtPrice.Text);
            theNewProduct2.eprice = Convert.ToDecimal(TextBox1.Text);
            theNewProduct2.ProductSeriesId = Guid.NewGuid();
            theNewProduct2.BrandId = new Guid("4a5642fc-5f0b-4101-b25f-0e16fde728cc");
            theNewProduct.keywords = this.TextBox2.Text;
            theNewProduct.contetnes = this.TextBox3.Text;
            DateTime thePublishDate2;
            if (DateTime.TryParse(this.txtPublishDate.Text, out thePublishDate2))
            {
                theNewProduct2.PublishDate = thePublishDate2;
            }
            else
            {
                theNewProduct2.PublishDate = DateTime.Today;
            }
            this.m_ProductManager.Insert(theNewProduct2);

        }
        if (cbCategory3.Checked)
        {
            Product theNewProduct3 = new Product();
            theNewProduct3.ProductCode = this.txtCode.Text.Trim();
            theNewProduct3.ProductId = Guid.NewGuid();
            theNewProduct3.Name = this.txtProductName.Text.Trim();
            theNewProduct3.ProductCategoryId = this.ddlProductCategory4.SelectedCategory.CategoryId;
            theNewProduct3.CreateTime = DateTime.Now;
            theNewProduct3.CreateUserId = new Guid("07C684D3-910F-44FD-9781-2DEE610B59E8");
            theNewProduct3.Description = this.fckBody.Value;
            theNewProduct3.Price = 0;
            theNewProduct3.LastViewTime = DateTime.Now;
            theNewProduct3.PictureUrl = this.imgProductPic.ImageUrl;
            theNewProduct3.Size = this.txtSize.Text;
            theNewProduct3.Views = 0;
            theNewProduct3.RecommendPicture1 = this.Image2.ImageUrl;
            theNewProduct3.RecommendPicture2 = this.Image3.ImageUrl;
            theNewProduct3.weight = Convert.ToInt32(this.txtWeight.Text);
            theNewProduct3.Price = Convert.ToDecimal(this.txtPrice.Text);
            theNewProduct3.eprice = Convert.ToDecimal(this.TextBox1.Text);
            theNewProduct3.ProductSeriesId = Guid.NewGuid();
            theNewProduct3.BrandId = new Guid("4a5642fc-5f0b-4101-b25f-0e16fde728cc");
            theNewProduct.keywords = this.TextBox2.Text;
            theNewProduct.contetnes = this.TextBox3.Text;
            DateTime thePublishDate3;
            if (DateTime.TryParse(this.txtPublishDate.Text, out thePublishDate3))
            {
                theNewProduct3.PublishDate = thePublishDate3;
            }
            else
            {
                theNewProduct3.PublishDate = DateTime.Today;
            }

            this.m_ProductManager.Insert(theNewProduct3);



        }
        if (CheckBox1.Checked)
        {
            Product theNewProduct4 = new Product();
            theNewProduct4.ProductCode = this.txtCode.Text.Trim();
            theNewProduct4.ProductId = Guid.NewGuid();
            theNewProduct4.Name = this.txtProductName.Text.Trim();
            theNewProduct4.ProductCategoryId = this.ddlProductCategory4.SelectedCategory.CategoryId;
            theNewProduct4.CreateTime = DateTime.Now;
            theNewProduct4.CreateUserId = new Guid("07C684D3-910F-44FD-9781-2DEE610B59E8");
            theNewProduct4.Description = this.fckBody.Value;
            theNewProduct4.Price = 0;// Convert.ToDecimal(this.txtPrice.Text.Trim());
            theNewProduct4.LastViewTime = DateTime.Now;
            theNewProduct4.PictureUrl = this.imgProductPic.ImageUrl;
            theNewProduct4.Size = this.txtSize.Text;
            theNewProduct4.Views = 0;
            theNewProduct4.RecommendPicture1 = this.Image2.ImageUrl;
            theNewProduct4.RecommendPicture2 = this.Image3.ImageUrl;
            theNewProduct4.weight = Convert.ToInt32(this.txtWeight.Text);
            theNewProduct4.Price = Convert.ToDecimal(this.txtPrice.Text);
            theNewProduct4.eprice = Convert.ToDecimal(this.TextBox1.Text);
            theNewProduct4.ProductSeriesId = Guid.NewGuid();
            theNewProduct4.BrandId = new Guid("4a5642fc-5f0b-4101-b25f-0e16fde728cc");
            theNewProduct.keywords = this.TextBox2.Text;
            theNewProduct.contetnes = this.TextBox3.Text;


            theNewProduct4.PublishDate = DateTime.Today;


            this.m_ProductManager.Insert(theNewProduct4);
        }

        #endregion
        this.ClearUI();

        this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('Insert Succeed!');", true);

        //清除checkbox,image
        this.cbCategory1.Checked = false;
        this.cbCategory2.Checked = false;
        this.cbCategory3.Checked = false;
        this.Image2.ImageUrl = "";
        this.Image2.Visible = false;
        this.Image3.ImageUrl = "";
        this.Image3.Visible = false;
        this.Image4.ImageUrl = "";
        this.Image5.ImageUrl = "";
    }

    private void ClearUI()
    {
        this.txtProductName.Text = string.Empty;
        this.txtNewCategoryName.Text = string.Empty;
        this.txtNewCategoryName.Text = string.Empty;
        this.fckBody.Value = string.Empty;
        this.imgProductPic.ImageUrl = string.Empty;
        this.imgProductPic.Visible = false;
        this.TextBox3.Text = "";
        this.TextBox2.Text = "";
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fuProductPic.HasFile)
        {
            string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["ProductPath"]);
            string theFileName = string.Empty;
            if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
            {
                theFileName = Guid.NewGuid().ToString() + fuProductPic.FileName.Substring(this.fuProductPic.FileName.LastIndexOf('.'));
            }
            else
            {
                theFileName = this.Request.QueryString["PID"] + fuProductPic.FileName.Substring(this.fuProductPic.FileName.LastIndexOf('.'));
            }

            this.fuProductPic.SaveAs(theFilePath + theFileName);

            this.imgProductPic.ImageUrl = ConfigurationManager.AppSettings["ProductPath"] + theFileName;
            this.imgProductPic.Visible = true;
            this.txtProductPic.Text = this.imgProductPic.ImageUrl;
        }
    }

    protected void btnSetProductPic_Click(object sender, EventArgs e)
    {
        this.imgProductPic.ImageUrl = this.txtProductPic.Text;
        this.imgProductPic.Visible = true;
    }

    protected void btnEditProduct_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(this.txtProductName.Text.Trim()))
        {
            this.ltlNeedProductName.Visible = true;
            return;
        }
        else
        {
            this.ltlNeedProductName.Visible = false;
        }

        Product theProduct = this.m_ProductManager.GetProductById(new Guid(this.Request.QueryString["PID"]));

        theProduct.Name = this.txtProductName.Text.Trim();

        //theProduct.ProductCategoryId = this.ddlProductCategory.SelectedCategory.CategoryId;

        if (!string.IsNullOrEmpty(ddlLv3Category.SelectedValue))
        {
            theProduct.ProductCategoryId = new Guid(this.ddlLv3Category.SelectedValue);
        }
        else
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('必须是3级分类!');", true);
            return;
        }
        if (txtPrice.Text.Trim() == "")
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('市场价格不能为空!');", true);
            return;
        }
        if (TextBox1.Text.Trim() == "")
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('真实价格不能为空!');", true);
            return;
        }

        theProduct.Description = this.fckBody.Value;
        theProduct.LastViewTime = DateTime.Now;
        theProduct.PictureUrl = this.imgProductPic.ImageUrl;
        theProduct.Size = this.txtSize.Text;
        theProduct.weight = int.Parse(string.IsNullOrEmpty(this.txtWeight.Text.Trim()) ? "0" : this.txtWeight.Text.Trim());
        theProduct.RecommendPicture1 = this.Image2.ImageUrl;
        theProduct.RecommendPicture2 = this.Image3.ImageUrl;
        theProduct.RecommendPicture3 = this.Image4.ImageUrl;
        theProduct.RecommendPicture4 = this.Image5.ImageUrl;
        theProduct.Price = Convert.ToDecimal(this.txtPrice.Text);
        theProduct.eprice = Convert.ToDecimal(this.TextBox1.Text);
        try
        {
            theProduct.PublishDate = Convert.ToDateTime(this.txtPublishDate.Text.Trim());
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('发布日期格式不正确!');", true);
            return;
        }
        theProduct.PublishDate = Convert.ToDateTime(this.txtPublishDate.Text.Trim());
        theProduct.keywords = this.TextBox2.Text;
        theProduct.contetnes = this.TextBox3.Text;
        List<ProductFit> theProductFitList = new List<ProductFit>();
        theProductFitList = this.GetProductFit(theProductFitList, this.rptHairFit);
        theProductFitList = this.GetProductFit(theProductFitList, this.rptSkinFit);
        this.m_ProductManager.Update();


        this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('Edit Succeed!');", true);
    }

    private List<ProductFit> GetProductFit(List<ProductFit> aProductFitList, Repeater aRepeater)
    {
        foreach (RepeaterItem theItem in aRepeater.Items)
        {
            string theScore = (theItem.FindControl("txtScore") as TextBox).Text;
            if (String.IsNullOrEmpty(theScore))
            {
                continue;
            }
            ProductFit theProductFit = new ProductFit();
            theProductFit.ProductFitId = Guid.NewGuid();
            theProductFit.CreateTime = DateTime.Now;
            theProductFit.FitCategoryId = new Guid((theItem.FindControl("hfCategoryId") as HiddenField).Value);

            theProductFit.Score = Convert.ToInt32((theItem.FindControl("txtScore") as TextBox).Text);
            aProductFitList.Add(theProductFit);
        }

        return aProductFitList;
    }

    protected void btnUploadCategoryPicture_Click(object sender, EventArgs e)
    {
        if (fuCategoryPicture.HasFile)
        {
            string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["CategoryPath"]);
            string theFileName = string.Empty;
            if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
            {
                theFileName = Guid.NewGuid().ToString() + fuCategoryPicture.FileName.Substring(this.fuCategoryPicture.FileName.LastIndexOf('.'));
            }
            else
            {
                theFileName = this.Request.QueryString["PID"] + fuCategoryPicture.FileName.Substring(this.fuCategoryPicture.FileName.LastIndexOf('.'));
            }

            this.fuCategoryPicture.SaveAs(theFilePath + theFileName);

            this.Image1.ImageUrl = ConfigurationManager.AppSettings["CategoryPath"] + theFileName;
            this.Image1.Visible = true;
            this.txtShowPicturePath.Text = this.Image1.ImageUrl;
        }
    }

    protected void btnShowPicture_Click(object sender, EventArgs e)
    {
        this.Image1.ImageUrl = this.txtProductPic.Text;
        this.Image1.Visible = true;
    }

    protected void btnNewCategory_Click(object sender, EventArgs e)
    {
        pnlNewCategorys.Visible = !this.pnlNewCategorys.Visible;
    }

    protected void cbCreateThirdCategory_CheckedChanged(object sender, EventArgs e)
    {
        this.panelSecoundCategory.Visible = !this.panelSecoundCategory.Visible;
    }

    protected void btnUpload2_Click(object sender, EventArgs e)
    {
        if (fuProductRecommend1.HasFile)
        {
            string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["ProductPath"]);
            string theFileName = string.Empty;
            if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
            {
                theFileName = Guid.NewGuid().ToString() + fuProductRecommend1.FileName.Substring(this.fuProductRecommend1.FileName.LastIndexOf('.'));
            }
            else
            {
                theFileName = Guid.NewGuid().ToString() + fuProductRecommend1.FileName.Substring(this.fuProductRecommend1.FileName.LastIndexOf('.'));
            }

            this.fuProductRecommend1.SaveAs(theFilePath + theFileName);

            this.Image2.ImageUrl = ConfigurationManager.AppSettings["ProductPath"] + theFileName;
            this.Image2.Visible = true;

        }
    }

    protected void btnUpload3_Click(object sender, EventArgs e)
    {
        if (fuProductRecommend2.HasFile)
        {
            string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["ProductPath"]);
            string theFileName = string.Empty;
            if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
            {
                theFileName = Guid.NewGuid().ToString() + fuProductRecommend2.FileName.Substring(this.fuProductRecommend2.FileName.LastIndexOf('.'));
            }
            else
            {
                theFileName = Guid.NewGuid().ToString() + fuProductRecommend2.FileName.Substring(this.fuProductRecommend2.FileName.LastIndexOf('.'));
            }

            this.fuProductRecommend2.SaveAs(theFilePath + theFileName);

            this.Image3.ImageUrl = ConfigurationManager.AppSettings["ProductPath"] + theFileName;
            this.Image3.Visible = true;

        }
    }

    protected void btnUpload4_Click(object sender, EventArgs e)
    {
        if (fuProductRecommend3.HasFile)
        {
            string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["ProductPath"]);
            string theFileName4 = string.Empty;
            if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
            {
                theFileName4 = Guid.NewGuid().ToString() + fuProductRecommend3.FileName.Substring(this.fuProductRecommend3.FileName.LastIndexOf('.'));
            }
            else
            {
                theFileName4 = Guid.NewGuid().ToString() + fuProductRecommend3.FileName.Substring(this.fuProductRecommend3.FileName.LastIndexOf('.'));
            }

            this.fuProductRecommend3.SaveAs(theFilePath + theFileName4);

            this.Image4.ImageUrl = ConfigurationManager.AppSettings["ProductPath"] + theFileName4;
            this.Image4.Visible = true;

        }
    }

    protected void btnUpload5_Click(object sender, EventArgs e)
    {
        if (fuProductRecommend4.HasFile)
        {
            string theFilePath = Server.MapPath(ConfigurationManager.AppSettings["ProductPath"]);
            string theFileName5 = string.Empty;
            if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
            {
                theFileName5 = Guid.NewGuid().ToString() + fuProductRecommend4.FileName.Substring(this.fuProductRecommend4.FileName.LastIndexOf('.'));
            }
            else
            {
                theFileName5 = Guid.NewGuid().ToString() + fuProductRecommend4.FileName.Substring(this.fuProductRecommend4.FileName.LastIndexOf('.'));
            }

            this.fuProductRecommend4.SaveAs(theFilePath + theFileName5);

            this.Image5.ImageUrl = ConfigurationManager.AppSettings["ProductPath"] + theFileName5;
            this.Image5.Visible = true;

        }
    }

    private void BindDDL()
    {
        BindLv1DDL();
        BindLv2DDL();
        BindLv3DDL();
    }

    private void BindLv1DDL()
    {
        //string topcategoryName = "Category_L";

        Category top = m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L);

        this.ddlLv1Category.DataSource = m_CategoryManager.GetSubCateogoriesBySupCategory(top);
        this.ddlLv1Category.DataBind();
    }

    private void BindLv2DDL()
    {
        //Category top = m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L);

        //Category lv1Category = m_CategoryManager.GetCategoryById(new Guid(this.ddlLv1Category.SelectedValue));
        if (!string.IsNullOrEmpty(this.ddlLv1Category.SelectedValue))
        {
            this.ddlLv2Category.DataSource = m_CategoryManager.GetSubCateogoriesBySupCategory(new Guid(this.ddlLv1Category.SelectedValue));
            this.ddlLv2Category.DataBind();
        }
        //else
        //{ 

        //}
    }

    private void BindLv3DDL()
    {
        if (!string.IsNullOrEmpty(this.ddlLv2Category.SelectedValue))
        {
            this.ddlLv3Category.DataSource = m_CategoryManager.GetSubCateogoriesBySupCategory(new Guid(this.ddlLv2Category.SelectedValue));
            this.ddlLv3Category.DataBind();
        }
    }

    //    if (this.ddlLv1Category.Items.Contains(new ListItem(value.SupCategory.SupCategory.Name, value.SupCategory.SupCategoryId.ToString())))
    //{
    //    this.ddlLv1Category.SelectedIndex = this.ddlLv1Category.Items.IndexOf(this.ddlLv1Category.Items.FindByValue(value.SupCategory.SupCategoryId.ToString()));
    //    this.ddlLv1Category_SelectedIndexChanged(null, null);
    //    this.ddlLv2Category.SelectedIndex = this.ddlLv2Category.Items.IndexOf(this.ddlLv2Category.Items.FindByValue(value.SupCategoryId.ToString()));
    //    this.ddlLv2Category_SelectedIndexChanged(null, null);
    //    this.ddlLv3Category.SelectedIndex = this.ddlLv3Category.Items.IndexOf(this.ddlLv3Category.Items.FindByValue(value.CategoryId.ToString()));
    //    this.ddlLv3Category_SelectedIndexChanged(null, null);
    //}

    protected void ddlLv1Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindLv2DDL();
        BindLv3DDL();
    }

    protected void ddlLv2Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindLv3DDL();
    }

}
